package net.lzzy.practices.aspects;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.utils.AspectUtils;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.web.models.ApiResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 *
 */
@Component
@Aspect
public class AdminControllerAspect {

//    @Before("adminAspect()")
//    public void beforeMethod(JoinPoint joinPoint) {
//        String methodName = joinPoint.getSignature().getName();
//        joinPoint.getArgs();
//        System.out.println("方法为:" + methodName + ", 这是一个前置测试，参数为："+joinPoint.getArgs()[0].toString());
//    }
//
//    @After("adminAspect()")
//    public void afterMethod(JoinPoint joinPoint) {
//        String methodName = joinPoint.getSignature().getName();
//        joinPoint.getArgs();
//        System.out.println("方法为:" + methodName + ", 这是一个后置测试，参数为："+joinPoint.getArgs()[0].toString());
//    }
//
//    @Around("adminAspect()")
//    public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable{
//        Object result=pjp.proceed(pjp.getArgs());
//        ApiResult apiResult = (ApiResult) result;
//        System.out.println(result.toString());
//        return result;
//    }
    @Pointcut("execution(* net.lzzy.practices.web.admin.*.*(..))")
    private void adminAspect(){}
    @Around("adminAspect()")
    public ApiResult checkAdmin(ProceedingJoinPoint joinPoint)throws Throwable{
        String token = AspectUtils.getToken(joinPoint);
        if(AuthUtils.illegalUser(token, new int[]{User.TYPE_ADMIN})){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), null);
        }
        return (ApiResult) joinPoint.proceed();
    }

}
